package com.fengba.nanzhi.tabbar04.wz;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.fengba.nanzhi.fengba.R;
import com.fengba.nanzhi.utils.Function;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by 89311 on 2018/4/28.
 */

public class MyTeamLevelAdapter extends RecyclerView.Adapter<MyTeamLevelAdapter.LinerViewHolder> {
    private Context context;
    private JSONArray jsonArray;
    private JSONObject jsob;

    public MyTeamLevelAdapter(Context context, JSONArray jsonArray) {
        this.context = context;
        this.jsonArray = jsonArray;
    }


    @Override
    public MyTeamLevelAdapter.LinerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyTeamLevelAdapter.LinerViewHolder(LayoutInflater.from(context).inflate(R.layout.wz_myteam_item_view,parent,false));
    }

    @Override
    public void onBindViewHolder(MyTeamLevelAdapter.LinerViewHolder holder, int position) {
        try {
             jsob = (JSONObject) jsonArray.get(position);
            Glide.with(context).load(jsob.get("headimg").toString()).into(holder.MyTeamTabtx);
            //姓名打码
            String account = Function.phoneHide4Start(jsob.get("account").toString());
            holder.MyTeamAccount.setText(account);

            if(jsob.get("realname").toString().trim().equals("null")) {
                holder.MyTeamUserName.setText("未实名");
            }else{
                String realname = Function.userNameHide(jsob.get("realname").toString());
                holder.MyTeamUserName.setText(realname);
            }
            String createtime = Function.TimeHide(jsob.get("createtime").toString());
            holder.MyTeamTabTime.setText(createtime);

            if(jsob.get("certified_status").toString().trim().equals("3")){
                holder.My_TEam_Type.setText("已认证");
                holder.My_TEam_Type.setTextColor(Color.parseColor("#70E2AA"));
            }else{
                holder.My_TEam_Type.setText("未完善");
                holder.My_TEam_Type.setTextColor(Color.parseColor("#000000"));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public int getItemCount() {
        if(jsonArray!=null){
            return jsonArray.length();
        }else{
            return 0;
        }
    }

    class LinerViewHolder extends RecyclerView.ViewHolder{
        private ImageView MyTeamTabtx;
        private TextView MyTeamUserName,MyTeamAccount,MyTeamTabTime,My_TEam_Type;
        public LinerViewHolder(View itemView) {
            super(itemView);
            MyTeamTabtx = itemView.findViewById(R.id.MyTeamTabtx);
            MyTeamUserName = itemView.findViewById(R.id.MyTeamUserName);
            MyTeamAccount = itemView.findViewById(R.id.MyTeamAccount);
            MyTeamTabTime = itemView.findViewById(R.id.MyTeamTabTime);
            My_TEam_Type = itemView.findViewById(R.id.My_TEam_Type);
        }
    }
    public static void showToast(final Activity ctx, final String msg) {
        // 判断是在子线程，还是主线程
        if ("main".equals(Thread.currentThread().getName())) {
            Toast.makeText(ctx, msg, Toast.LENGTH_SHORT).show();
        } else {
            // 子线程
            ctx.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(ctx, msg, Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

}
